package com.malou.client.seguridad.login;

import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.KeyListener;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Status;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormLayout;
import com.extjs.gxt.ui.client.widget.toolbar.FillToolItem;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyCodes;
import com.malou.client.MalouConstants;
import com.malou.client.seguridad.usuario.Usuario;
import com.malou.client.seguridad.usuario.Usuarios;

public class LoginDialog extends Dialog {

  protected TextField<String> userName;
  protected TextField<String> password;
  protected Button reset;
  protected Button login;
  public Status status;  
  private MalouConstants constants = GWT.create(MalouConstants.class);
 

  public LoginDialog() {
	Usuarios.dialog = this;
    FormLayout layout = new FormLayout();
    layout.setLabelWidth(90);
    layout.setDefaultWidth(155);
    setLayout(layout);
    
    setButtonAlign(HorizontalAlignment.LEFT);
    setButtons("");
    setIcon(IconHelper.createStyle("user"));
    setHeading(constants.loginIniciarSesion());
    setModal(true);
    setBodyBorder(true);
    setBodyStyle("padding: 8px;background: none");
    setWidth(300);
    setResizable(false);

    userName = new TextField<String>();
    userName.setMinLength(4);
    userName.setFieldLabel(constants.loginUsuario());
    userName.setAllowBlank(false);
    userName.addKeyListener(new KeyListener(){
		public void componentKeyDown(ComponentEvent event){
			if(event.getKeyCode() == KeyCodes.KEY_ENTER){									
				password.focus();
			}
		}
	});
    add(userName);

    password = new TextField<String>();
    password.setMinLength(3);
    password.setPassword(true);
    password.setFieldLabel(constants.loginClave());
    password.setAllowBlank(false);
    password.addKeyListener(new KeyListener(){
		public void componentKeyDown(ComponentEvent event){
			if(event.getKeyCode() == KeyCodes.KEY_ENTER){
				if(validate())
					onSubmit();
			}
		}
	});
    add(password);

    setFocusWidget(userName);

  }

  @Override
  protected void createButtons() {
    super.createButtons();
    
    if (constants == null) {
    	constants = GWT.create(MalouConstants.class);
	}
    
    status = new Status();
    status.setBusy(constants.loginValidando());
    status.hide();
    status.setAutoWidth(true);
    getButtonBar().add(status);
    
    getButtonBar().add(new FillToolItem());
    
    reset = new Button(constants.loginLimpiar());
    reset.addSelectionListener(new SelectionListener<ButtonEvent>() {
      public void componentSelected(ButtonEvent ce) {
        userName.reset();
        password.reset();
        validate();
        userName.focus();
      }

    });

    login = new Button(constants.loginEntrar());    
    login.addSelectionListener(new SelectionListener<ButtonEvent>() {
      public void componentSelected(ButtonEvent ce) {
    	  if(validate())
    		  onSubmit();
      }
    });
    
    addButton(login);
    addButton(reset);    
  }

  private void onSubmit() {
    status.show();
    getButtonBar().disable();
    
	Usuario tda = new Usuario();	
	
	tda.setUsuario(userName.getValue());
	tda.setClave(password.getValue());
	
	Usuarios.isValidLogin(tda);
	
	
  }  


  private Boolean validate() {
	  boolean user = userName.isValid();
	  boolean pw = password.isValid();
    return  user && pw;
  }
  
 

}
